import pandas as pd
import re
import numpy as np
from pandas.core.frame import DataFrame


raw = pd.read_csv("金庸-射雕英雄传txt精校版.txt",
                      names=['txt'], sep='aaa', encoding="GBK", engine='python')
# 章节判断用变量预处理
def chapter_pre(raw):
    def m_head(tmpstr):
        return tmpstr[:1]

    def m_mid(tmpstr):
        return tmpstr.find("回 ")

    raw['head'] = raw.txt.apply(m_head)
    raw['mid'] = raw.txt.apply(m_mid)
    raw['len'] = raw.txt.apply(len)


# 章节判断
def chapter_judge(raw):
    chapnum = 0
    for i in range(len(raw)):
        if raw['head'][i] == "第" and raw['mid'][i] > 0 and raw['len'][i] < 30 :
            chapnum += 1
        if chapnum >= 40 and raw['txt'][i] == "附录一：成吉思汗家族" :
            chapnum = 0
        raw.loc[i, 'chap'] = chapnum
    del raw['head']
    del raw['mid']
    del raw['len']


#选取需要处理的章节
def chapter_pick(raw):
    tmpchap = raw[raw['chap'] == 1].copy()
    tmpchap.reset_index(drop=True, inplace=True)
    tmpchap['paraidx'] = tmpchap.index
    tmpparas=[]
    for i in range(len(raw[raw['chap'] == 1])):
        tmppara = tmpchap[tmpchap['chap'] == 1].copy()
        tmpstr = tmppara.txt[i]
        # print(tmpstr)
        sentences = re.findall('(.*?[？。！；：](’|”)?)', tmpstr)
        data = pd.DataFrame(sentences, columns=['txt', 'paraidx'])
        data['paraidx'] = np.where(data['paraidx'] == '', i, data['paraidx'])
        print(data)


def cut_sent(para):
    para = re.sub('([。！？\?])([^”’])', r"\1\n\2", para)  # 单字符断句符
    para = re.sub('(\.{6})([^”’])', r"\1\n\2", para)  # 英文省略号
    para = re.sub('(\…{2})([^”’])', r"\1\n\2", para)  # 中文省略号
    para = re.sub('([。！？\?][”’])([^，。！？\?])', r'\1\n\2', para)#中文双引号
    para = para.rstrip()  # 段尾如果有多余的\n就去掉它
    return para.split("\n")


if __name__=="__main__":
    #1.读入为按整句分案例行的数据框格式，并用另一个变量标识其所在段落的流水号。
    #实现
    chapter_pre(raw)
    chapter_judge(raw)
    tmppara_list = chapter_pick(raw)

    sentence_list = []


    print("段落列表为：",sentence_list)

    #2.数据框转换为以整段为成员的list格式。
    #实现
    length_list=[]
    for i in range(len(sentence_list)):
        length = len(sentence_list[i])
        length_list.append(length)

    frame={"句子内容":sentence_list,
           "句子长度":length_list}
    data=DataFrame(frame)
